package com.guigui.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeCloseModel;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.guigui.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2016092200568607";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCTQjBY5PvTZL+Iv/u5U+l9fKB0WsxAZ7MaIlPmlojwacNSuOnVPPex7zjEQoNK7CVeCVDXSBYv8Frlwi0Z9LbKQMbV4FTPgV/VxH7DzqoLDiWIR7FL2RUw67DP+DtDN7r6qxX/3/w5B0nVaqiPv7zAWnOOj+C4PLMGjr0wRfrK4fGuO7Q4v5xfz8MyDnxrxpGXCjc0UTUr+vkXQEscg/raUSjWa6oln454WlMPE3+UZ5O1xpGJHB7wY6AK0MvPuGG6DaGaKnhy9Kf59YQYaKvUjrhlUwdu14xqT7nIZNYZ/m9WIqlRKFTeDE7Vs3CKyiVIGK/LM5mzNrtral31MM5lAgMBAAECggEAEEW+JfSXx1JYv/nHDUGyiYVeDb2C2JMc/YRRlSeJVle5l8iLPl8QV/9BXJ6k94CE2JFa948ltIJw6Pa+GSIrmMnzruqtNIjtWCfwgh6yN6qGhzq04tbIOrGwxip+Ef10kgKZ2b6cloixpf21Rr0/VFoqbtSDVpiOh+j6Cv2Qqx6E8E1N71gM0xzZIy3TdgA8rMQFyesrRDvrkoGDXWBNlcdr9l6HXVfFEMzHQY+MIhKWZ+1sCrYp/ThKLBXKwotN8YaD216blAijE6K9gnC2ZiHx7Mkb2scUtFMCJXQNDPXT1Z3dRO2L61QKkKA0W3BwJedE9e/Sw2K0raP3FRLmQQKBgQDqyZ/4UhifZT2Oo5hnPrDseXRuPkagBtxfIS49gX/i/fjDXfOaqia/JipICELgV3pU+NY5yCLjubV9jKruPWD4RIc3XTp+n22Y9lzgk4hBsLnNMl69URaXZSsebMfQ1Cr2s9uyC6QNjRrOp1t+spT8XiDMVD0C0s5DTOJukFdfuQKBgQCgkB1jFJtjPRwzSG98MLNrrDr8/1b3S7/xVGo4EGM4POJWs7J3jiq+eYBs7q2jEbRQ0ZbExDfqkXKOpgXzVcAZeDIFx32NR/XyFrD9Q8FZJHSyHo519BRCLGQT4gEuzs8haU4BJDBUSzfZhUaWuf2/IvjZB4Ot4bMN2Myg882CDQKBgQDqEx3zehWmRE78uEknFa9Z8zSK/Hd4754hEXmCRGt1qPKsBCzsE5sRk0EwE1xpQY1qzbL2ZHu0Qd+ZTfbUEkls+xU7syywNM4VY/jwxOVhaIm08/TUFdXKFkjIynbj0cMLtgDgHw7tm7IQOXXUeMJkYIueapVxDS4qlqpgxHL3sQKBgBxUMQspOJ1Sy+X0cs8bnJxzv9ihiIuLTutadH89zPPAMkMP434alEoVD+aoWfR6jDDyOcKvyBnX3blza3tadtFHlrPUVb+Wobs767BTwwpiV0CSCZqgHO7r/CBR8Nl8XalMdhnXc7t7pff3/tNbL30Iw5UvMTl9p+TaiogfW/QFAoGBAKWrNFIcXTVcHJDLdq36fUrdIZ/rY4THbmRLD4v4QCdXj6wriRT9fbaY1iz6OFNTbfVgIFJkULpTUHnC6vR79Ckkut2SMZG6VN9Yi70tZqYsu2FY8xOxJ48uO6xsDS0Gejnv1wvCRyIXX+PuQmwIELrryRSZ8Jau6Fbn4kaO9Vh0";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArPLEDJknjPu9GKyBlp/+8rYqcX7TIREOXuV5IeK7INzYUVUcnO5d1nPgi98vNQoQchr2pSvykC1w1TJqj5SfECElymM1a72x0xzNqazokHqCJgEdmumOnTaOvpUey19bHXMqXAq9sHpcfUHtgBbjM+AGxwrV9hVooB7x7n0qdziRvqd0iL1Se6iDYTmG0Cdo7L8I2IbJ2Uo1Vqv/jrGI/iOXHNRo/BKiUamZzqXsr/wsTII28R4P7Q4Z17kA9odxnognZHBa0779vzFb45QCFLxDguen+7cY/xULRdpgzUY+YUMmeMCq0fMAiWz7BUiQeyjoL6ERVMf4AJeWO1AyAQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url;

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 超时关闭订单时间
    private String timeout = "2m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }

    /**
     * 收单。参数二选一。
     * @param out_trade_no 商户订单号
     * @param trade_no 支付宝交易号
     * @return
     * @throws AlipayApiException
     */
    public String close(String out_trade_no, String trade_no) throws AlipayApiException {
        //商户订单号和支付宝交易号不能同时为空。 trade_no、  out_trade_no如果同时存在优先取trade_no
        //商户订单号，和支付宝交易号二选一
//        String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        //支付宝交易号，和商户订单号二选一
//        String trade_no = new String(request.getParameter("WIDtrade_no").getBytes("ISO-8859-1"),"UTF-8");
        /**********************/
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        AlipayClient client = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json", charset, alipay_public_key, sign_type);
        AlipayTradeCloseRequest alipay_request = new AlipayTradeCloseRequest();

        AlipayTradeCloseModel model = new AlipayTradeCloseModel();
        model.setOutTradeNo(out_trade_no);
        model.setTradeNo(trade_no);
        alipay_request.setBizModel(model);

        AlipayTradeCloseResponse alipay_response = client.execute(alipay_request);

        String body = alipay_response.getBody();

        return body;
    }
}
